﻿#pragma once
#include<iostream>

using namespace std;


enum colour
{
	RED,
	BLACK
};



template<class K, class V>
struct RBTreeNode
{
	pair<K, V> _val;
	RBTreeNode<K, V>* left;
	RBTreeNode<K, V>* right;
	RBTreeNode<K, V>* parent;

	colour _col;
	RBTreeNode(const pair<K, V>& val)
		:_val(val)
		, left(nullptr)
		, right(nullptr)
		, parent(nullptr)
	{

	}


};



template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;

public:

	bool Insert(const pair <K, V> val)
	{
		if (root == nullptr)
		{
			root = new Node(val);
			root->_col = BLACK;
			return true;
		}
		Node* cur = root;
		Node* Parent = nullptr;
		while (cur)
		{
			if (cur->_val.first < val.first)
			{
				Parent = cur;
				cur = cur->right;
			}
			else if (cur->_val.first > val.first)
			{
				Parent = cur;
				cur = cur->left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(val);
		if (Parent->_val.first < val.first)
		{
			Parent->right = cur;
		}
		else
		{
			Parent->left = cur;
		}
		cur->parent = Parent;
		cur->_col = RED;
		while (Parent && Parent->_col == RED)
		{
			Node* grandfather = Parent->parent;

			//父节点为祖父节点的左节点时
			//    g
			//  p  u
			//c
			if (grandfather->left == Parent)
			{

				Node* uncle = grandfather->right;
				//叔叔存在且为红
				if (uncle && uncle->_col == RED)
				{
					Parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					Parent = cur->parent;
				}
				//叔叔不存在或者为黑
				else
				{
					//当c为p的左节点时
					//    g
					//  p  u
					//c
					if (Parent->left == cur)
					{
						RotateR(grandfather);
						grandfather->_col = RED;
						Parent->_col = BLACK;
					}
					//当c为p的右节点时
					//     g
					//   p   u
					//    c
					else
					{
						RotateL(Parent);
						RotateR(grandfather);
						grandfather->_col = RED;
						cur->_col = BLACK;

					}
					break;


				}

			}
			//父节点为祖父节点的右节点时
			//    g
			//  u   p
			//       c
			else
			{

				Node* uncle = grandfather->left;
				//叔叔存在且为红
				if (uncle && uncle->_col == RED)
				{
					Parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					Parent = cur->parent;
				}
				//叔叔不存在或者为黑
				else
				{
					//当c为p的右节点时
					//    g
					//  u   p
					//       c
					if (Parent->right == cur)
					{
						RotateL(grandfather);
						grandfather->_col = RED;
						Parent->_col = BLACK;
					}
					//当c为p的左节点时
					//    g
					//  u   p
					//     c
					else
					{
						RotateR(Parent);
						RotateL(grandfather);
						grandfather->_col = RED;
						cur->_col = BLACK;

					}
					break;


				}

			}


		}


		root->_col = BLACK;
		return true;

	}



	Node* Find(const K& val)
	{
		if (root == nullptr)
		{
			return nullptr;
		}
		Node* cur = root;
		while (cur)
		{
			if (cur->_val.first < val)
			{
				cur = cur->left;
			}
			else if (cur->_val.first > val)
			{
				cur = cur->right;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}


	void InOrder()
	{
		_InOrder(root);
		cout << endl;
	}

	int  Height()
	{
		return _Height(root);
	}


	int Size()
	{
		return _Size(root);
	}


	



	bool Check(Node* root, int blackNum, const int refNum)
	{
		if (root == nullptr)
		{
			// 前序遍历⾛到空时，意味着⼀条路径⾛完了
			//cout << blackNum << endl;
			if (refNum != blackNum)
			{
				cout << "存在⿊⾊结点的数量不相等的路径" << endl;
				return false;
			}
			return true;
		}
		// 检查孩⼦不太⽅便，因为孩⼦有两个，且不⼀定存在，反过来检查⽗亲就⽅便多了
		if (root->_col == RED && root->parent->_col == RED)
		{
			cout << root->_val.first << "存在连续的红⾊结点" << endl;
			return false;
		}
		if (root->_col == BLACK)
		{
			blackNum++;
		}
		return Check(root->left, blackNum, refNum)
			&& Check(root->right, blackNum, refNum);
	}
	bool IsBalance()
	{
		if (root == nullptr)
			return true;
		if (root->_col == RED)
			return false;
		// 参考值
		int refNum = 0;
		Node* cur = root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++refNum;
			}
			cur = cur->left;
		}
		return Check(root, 0, refNum);
	}

	


private:
	Node* root = nullptr;


	void RotateR(Node* Parent)
	{
		Node* subL = Parent->left;
		Node* subLR = subL->right;
		if (subLR != nullptr)
		{
			subLR->parent = Parent;
		}
		Node* tmpNode = Parent->parent;
		Parent->left = subLR;
		Parent->parent = subL;
		subL->right = Parent;
		if (tmpNode != nullptr)
		{
			if (tmpNode->left == Parent)
			{
				tmpNode->left = subL;
			}
			else
			{
				tmpNode->right = subL;
			}
			subL->parent = tmpNode;
		}
		else
		{
			root = subL;
			subL->parent = nullptr;

		}

	}



	void RotateL(Node* Parent)
	{
		Node* subR = Parent->right;
		Node* subRL = subR->left;
		if (subRL != nullptr)
		{
			subRL->parent = Parent;
		}
		Node* tmpNode = Parent->parent;
		Parent->right = subRL;
		Parent->parent = subR;
		subR->left = Parent;
		if (tmpNode != nullptr)
		{
			if (tmpNode->left == Parent)
			{
				tmpNode->left = subR;
			}
			else
			{
				tmpNode->right = subR;
			}
			subR->parent = tmpNode;
		}

		else
		{
			root = subR;
			subR->parent = nullptr;

		}

	}


	void _InOrder(Node* cur)
	{
		if (cur == nullptr)
		{
			return;
		}
		_InOrder(cur->left);
		cout << cur->_val.first << ":"<<cur->_val.second<<endl;
		_InOrder(cur->right);
	}



	int _Height(Node* cur)
	{
		if (cur == nullptr)
		{
			return 0;
		}
		int Left = _Height(cur->left);
		int Right = _Height(cur->right);
		return Left > Right ? Left + 1 : Right + 1;
	}


	

	int _Size(Node* cur)
	{
		if (cur == nullptr)
		{
			return 0;
		}

		return 1 + _Size(cur->left) + _Size(cur->right);

	}


};